﻿using System;
using System.Web;
using System.Text.RegularExpressions;

namespace WebApplication
{
    public class HttpModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }
        
        public void Init(HttpApplication context)
        {
            // Below is an example of how you can handle LogRequest event and provide 
            // custom logging implementation for it
            context.BeginRequest += new EventHandler(OnBeginRequest);
            context.EndRequest += new EventHandler(OnEndRequest);
        }

        #endregion

        public void OnBeginRequest(Object source, EventArgs e)
        {
            //custom logging logic can go here
            HttpApplication app = source as HttpApplication;
            if (app.Request.CurrentExecutionFilePathExtension == ".js")
            {
                string Reurl = app.Request.Path;
                if (Reurl.ToLower().Contains("jquery.js")&&!String.IsNullOrEmpty(app.Request.QueryString["v"]))
                {
                    //Regex reg = new Regex("jquery.js?");
                    app.Context.RewritePath(Reurl.Replace("jquery.js", String.Format("jquery-{0}.min.js", app.Request.QueryString["v"])));
                }
            }
            else if (app.Request.CurrentExecutionFilePathExtension == ".zip")
            {
                //app.Response.TransmitFile(app.Server.MapPath("/CTE Beta1.3.zip"));
                
            }
        }

        public void OnEndRequest(Object source, EventArgs e)
        {
            HttpApplication app = source as HttpApplication;
            if (app.Request.CurrentExecutionFilePathExtension == ".zip")
            {
                var response = app.Response;
            }
        }
        
    }
}
